﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace Project.Ycm20250401.Service._20250418.Domain.Entities
{
    /// <summary>
    /// 用户银行卡
    /// </summary>
    public class UserBank
    {
        public string UserId { get; private set; }
        private readonly List<BankItem> _bankItems = new List<BankItem>();
        public IReadOnlyCollection<BankItem> BankItems => _bankItems.AsReadOnly();

        public UserBank(string userId)
        {
            UserId = userId;
        }
       /// <summary>
       /// 绑定银行卡
       /// </summary>
       /// <param name="cardId"></param>
       /// <param name="bankName"></param>
       /// <param name="bankCode"></param>
       /// <param name="bankAccount"></param>
        public void BindBankItem(BankItem bankItem)
        {
            if (string.IsNullOrEmpty(bankItem.BankName))
            {
                throw new ArgumentException("银行名称不能为空！");
            }
            if (string.IsNullOrEmpty(bankItem.BankCode))
            {
                throw new ArgumentException("银行代码不能为空！");
            }
            if (string.IsNullOrEmpty(bankItem.BankAccount))
            {
                throw new ArgumentException("银行账号不能为空！");
            }
            if (_bankItems.Any(bc => bc.BankCode == bankItem.BankCode))
            {
                throw new InvalidOperationException("银行卡已绑定");
            }
            _bankItems.Add(bankItem);
        }
        /// <summary>
        /// 解绑银行卡
        /// </summary>
        /// <param name="cardId"></param>
        public void UnBindBankItem(string cardId)
        {
            var items = _bankItems.FirstOrDefault(x => x.cardId == cardId);
            if (items == null)
            {
                throw new ArgumentException("银行卡不存在！");
            }
            _bankItems.Remove(items);
        }
    }
    /// <summary>
    /// 银行卡信息
    /// </summary>
    /// <param name="cardId"></param>
    /// <param name="BankName"></param>
    /// <param name="BankCode"></param>
    /// <param name="BankAccount"></param>
    public record  BankItem( string cardId, string BankName, string BankCode, string BankAccount);
  
}